/**
 * 获取本地存储
 * @param key
 * @param val
 * @param boole
 * @returns {*}  根据参数增删改
 */
let getStore = (key, val) => {
  if (val === 'DELETE') {
    wx.removeStorageSync(key)
  } else if (val) {
    wx.setStorageSync(key, val)
  } else {
    return wx.getStorageSync(key)
  }
}
let navigator = (url) => {
  if (typeof url === 'string') {
    let e = url.indexOf('main') - 1
    let s = url.indexOf('pages') + 6
    url = url.replace('main', url.slice(s, e))
    wx.navigateTo({
      url: url
    })
  } else if (typeof url === 'number') {
    wx.navigateBack({
      delta: url
    })
  }
}
/**
 * 确认取消模态框
 * @param title 标题
 * @param content  内容
 * @returns {Promise}
 */

let showModal = (title, content, success) => {
  wx.showModal({
    title,
    content,
    success
  })
}

/**
 *  操作错误
 * @param title 提示内容
 * @param content
 */
let errToast = (title) => {
  wx.showToast({
    title,
    image: '/static/images/close.png'
  })
}

/**
 * 操作成功
 * @param title
 */
let successToast = (title, success) => {
  wx.showToast({
    title,
    icon: 'success',
    success
  })
}

/**
 * 操作成功
 * @param title
 */
let textToast = (title, success) => {
  wx.showToast({
    title,
    icon: 'none',
    success
  })
}

/**
 * 手机号验证
 * @param val
 * @returns {boolean}
 */
let testPhone = (val) => {
  if (!/^1[3|4|5|6|7|8|9][0-9]{9}$/.test(val)) {
    if (val === '') {
      textToast('手机号不能为空')
    } else {
      textToast('请输入正确格式手机号')
    }

    return true
  }
}

let loading = (bool) => {
  if (bool) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
  } else {
    wx.hideLoading()
  }
}
/**
 * 设置导航条名字
 * @param title
 */
let setNavigationBarTitle = function (title) {
  wx.setNavigationBarTitle({
    title
  })
}

/**
 * 关闭所有页面
 * @param title
 */
let reLaunch = function (url) {
  let e = url.indexOf('main') - 1
  let s = url.indexOf('pages') + 6
  url = url.replace('main', url.slice(s, e))

  wx.reLaunch({
    url: url
  })
}

/**
 * 关闭当前页面
 * @param title
 */
let redirectTo = function (url) {
  let e = url.indexOf('main') - 1
  let s = url.indexOf('pages') + 6
  url = url.replace('main', url.slice(s, e))

  wx.redirectTo({
    url: url
  })
}
let switchTab = function (url) {
  wx.switchTab({
    url
  })
}
module.exports = {
  getStore,
  navigator,
  reLaunch,
  switchTab,
  redirectTo,
  textToast,
  showModal,
  errToast,
  successToast,
  testPhone,
  loading,
  setNavigationBarTitle
}
